<?php
namespace Home\Controller;
use Think\Controller;
class ParameterController extends CommonController {
    public function data_list(){
        $limit = I("pageSize", 30, "intval");
        $page = I("pageCurrent", 1, "intval");
        $search_words = I('search_words');
        $condition['pa_name'] = array('like','%'.$search_words.'%');
        $this->assign('search_words',$search_words);
        $parameter_model = D("Parameter");
        $count = $parameter_model->get_count($condition);
        $dataList = $parameter_model->get_parameter_list($condition, $limit, $page);
        $this->assign("count",$count);
        $this->assign("dataList",$dataList);
    	$this->display();
    }
    public function operation(){
        $ot = I("ot");
        $pa_id = $_GET['pa_id'];
        $parameter_model = D("Parameter");
        if($ot==null||$ot==''){
            $ot = $_GET["ot"];
        }
        if($ot=='add'&&$pa_id==null){
            if(IS_POST){
                $data['pa_name'] = $_POST["pa_name"];
                $data['pa_key'] = $_POST["pa_key"];
                $result = $parameter_model->add($data);
                if($result){
                    $parameterValue_model = D("ParameterValue");
                    $d['pav_name'] = 'default';
                    $d['pav_value'] = '0';
                    $d['pav_pa_id'] = $result;
                    $parameterValue_model->add($d);

                    $orderList = M("Order")->where(array('or_status'=>array("neq",2)))->select();
                    foreach ($orderList as $key => $value) {
                        $run = true;
                        $orderParams_json = $value['or_param'];
                        $orderParams = json_decode($orderParams_json,true);
                        $orderParams[$_POST["pa_key"]] = '0';
                        M("Order")->where(array('or_id'=>$value['or_id']))->save(array('or_param'=>json_encode($orderParams)));
                    }

                    exit(success('成功！',true));
                }else{
                    exit(error('失败！'));
                }
            }else{
                $this->assign("ot",'add');
                $this->display();
            }
            $this->display();
        }if($ot=='edit'){
            if(IS_POST&&$_POST['pa_id']!=null){
                $condition['pa_id'] = $_POST['pa_id'];
                $data['pa_name'] = $_POST["pa_name"];
                $data['pa_key'] = $_POST["pa_key"];

                $where['pa_id'] = $_POST['pa_id'];
                $parameter = $parameter_model->where($where)->find();

                /* 更改未完成订单参数 */
                $orderList = M("Order")->where(array('or_status'=>array("neq",2)))->select();
                foreach ($orderList as $key => $value) {
                    $run = true;
                    $orderParams_json = $value['or_param'];
                    $orderParams = json_decode($orderParams_json,true);
                    foreach ($orderParams as $k => $v) {
                        if($run){
                            if($k == $parameter['pa_key']){
                                $d = $orderParams[$k];
                                unset($orderParams[$k]);
                                $orderParams[$_POST["pa_key"]] = $d;
                            }
                        }
                    }
                    M("Order")->where(array('or_id'=>$value['or_id']))->save(array('or_param'=>json_encode($orderParams)));
                }

                $result = $parameter_model->where($condition)->save($data);
                if($result){
                    exit(success('成功！',true));
                }else{
                    exit(error('失败！'));
                }
            }else{
                $condition['pa_id'] = $pa_id;
                $data = $parameter_model->where($condition)->find();
                $this->assign("ot",'edit');
                $this->assign("data",$data);
                $this->display();
            }
        }else if($pa_id!=null&&$ot=='del'){
            $condition['pa_id'] = $pa_id;
            $parameter = $parameter_model->where($condition)->find();
            $result = $parameter_model->where($condition)->delete();

            /* 更改未完成订单参数 */
            $orderList = M("Order")->where(array('or_status'=>array("neq",2)))->select();
            foreach ($orderList as $key => $value) {
                $run = true;
                $orderParams_json = $value['or_param'];
                $orderParams = json_decode($orderParams_json,true);
                foreach ($orderParams as $k => $v) {
                    if($run){
                        if($k == $parameter['pa_key']){
                            unset($orderParams[$k]);
                        }
                    }
                }
                M("Order")->where(array('or_id'=>$value['or_id']))->save(array('or_param'=>json_encode($orderParams)));
            }


            if($result){
                exit(success('成功！',false));
            }else{
                exit(error('失败！'));
            }
        }else{
            exit(error('参数错误！'));
        }
    }
}